package com.mms.utsc.smartnet;

import android.app.AlarmManager;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.os.PowerManager;
import android.widget.Toast;

public class UpdateAnnouncements extends BroadcastReceiver {

	@Override
	public void onReceive(Context context, Intent intent) {
		// TODO Auto-generated method stub
		PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
		PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "");
		wl.acquire();

		// Put here YOUR code.
		DBHandler db = new DBHandler(null, context);
		try {
			db.updateAnnouncements();
			db.close();
			// Toast.makeText(context, "updated announcements",
			// Toast.LENGTH_LONG).show();
		} catch (Exception e) {
			// Toast.makeText(context, "Did NOT update announcements",
			// Toast.LENGTH_LONG).show();
			e.printStackTrace();
		}
		db.close();

		wl.release();
	}

	void schedUpdate(Context context, long time) {
		AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
		Intent intent = new Intent(context, UpdateAnnouncements.class);
		PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
		// Millisec * Second * Minute * Hour
		// interval in milliseconds between alarms
		// am.setRepeating(AlarmManager.RTC_WAKEUP, time, 1000 * 60 * 60 * 24,
		// pendingIntent);
		am.setRepeating(AlarmManager.RTC_WAKEUP, time, 1000 * 60 * 60 * 24, pendingIntent);
	}

	void cancelSchedUpdate(Context context) {
		Intent intent = new Intent(context, UpdateAnnouncements.class);
		PendingIntent sender = PendingIntent.getBroadcast(context, 0, intent, 0);
		AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
		alarmManager.cancel(sender);
	}

}
